package org.bmind.datasword.config.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.bmind.datasword.config.ConfigManager;
import org.bmind.datasword.config.dto.DConfig;
import org.bmind.datasword.config.dto.DMonoAnalisys;
import org.bmind.datasword.config.dto.DriverClassName;
import org.bmind.datasword.utils.setup.Ordb2Pojo;

import com.orientechnologies.orient.core.command.OCommandRequest;
import com.orientechnologies.orient.core.db.object.ODatabaseObject;
import com.orientechnologies.orient.core.db.object.ODatabaseObjectTx;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery;
import com.orientechnologies.orient.core.sql.query.OSQLQuery;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;



public class OrientConfig implements ConfigManager {
	private ODatabaseObject orDb;
	private static Map<String, Object> lparameters = new HashMap<String, Object>();
	
	@Override
	public void init(Map<String, Object> parameters) throws Exception {
		
		lparameters = parameters;

		if (parameters.containsKey("url")) {
			this.orDb = new ODatabaseObjectTx((String) parameters.get("url"));

			if (parameters.containsKey("user")) {
				if (parameters.containsKey("password")) {
					this.orDb.open((String) parameters.get("user"),	(String) parameters.get("password"));
				}
			}
		}
	}
	
	@Override
	public DConfig getConfig() {

		DConfig dConfig = new DConfig();

		Ordb2Pojo.init(orDb);
		dConfig = Ordb2Pojo.dconfig2pojo();
		orDb.close();
		return dConfig;
		

	}

	@Override
	public DConfig getConfig(Collection<String> areas, Collection<String> roles) {
		DConfig dconfig = new DConfig();
		try {
			init(lparameters);

			Ordb2Pojo.init(orDb);
			dconfig = Ordb2Pojo.dconfig2pojo(areas, roles);

			orDb.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dconfig;
	}

	@Override
	public Collection<String> getAreas(Collection<String> roles) {
		
		return (Collection<String>)orDb.query(new OSQLSynchQuery<Object>("select * from  Areas  where roles in["+ roles +"]"));
		
	}

	@Override
	public void setConfig(DConfig config) {orDb.save(config);}

	@Override
	public void removeConfig(ODatabaseObject orDb) {
		Collection<ODocument> ltrid = new ArrayList<ODocument>();
		Collection<ODocument> llrid = new ArrayList<ODocument>();

		try {
			init(lparameters);

			for (DriverClassName ClassName : DriverClassName.values()) {
				llrid = (Collection<ODocument>)orDb.query(new OSQLSynchQuery<ODocument>("select @rid from " + ClassName.toString()));
				ltrid.addAll(llrid);

			}

			for (ODocument odoc : ltrid) {
				System.out.println(getRID(odoc.toString()));
				try {
					orDb.command(new OSQLAsynchQuery("delete from " + getRID(odoc.toString()))).execute();
				} catch (Exception e) {

				}
			}
		//	orDb.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	 private static String getClassName(Class c) {
		    String FQClassName = c.getName();
		    int firstChar;
		    firstChar = FQClassName.lastIndexOf ('.') + 1;
		    if ( firstChar > 0 ) {
		      FQClassName = FQClassName.substring ( firstChar );
		      }
		    return FQClassName;
		    }
	 
	 private static String getRID(String ORID){
		 
		int firstChar = ORID.lastIndexOf("#");
		int lastChar = ORID.lastIndexOf("}");
		return ORID.substring(firstChar, lastChar);
	 }





	
	 
}
